import { createMultiTypedReconnectableWebSocket, jsonRequest, ReconnectableWebsocket } from "@/requests"
import { TerminalType } from "./models"
import { formatURL } from "@/utils"
export class PtyAPI {
    componentName: string
    ws: ReconnectableWebsocket
    constructor(componentName: string) {
        this.componentName = componentName
        this.ws = createMultiTypedReconnectableWebSocket(this.formatMethodURL("/websocket"), {
            msgInList: true
        })
    }
    formatMethodURL(method: string) {
        return formatURL(this.componentName, method)
    }
    createPTY = async (cmd: string | string[], name: string): Promise<TerminalType> => {
        return (await jsonRequest.post(this.formatMethodURL("/create"), { cmd, name })).data
    }
    resizePTY = async (termID: string, rows: number, cols: number): Promise<void> => {
        await jsonRequest.post(this.formatMethodURL("/resize"), { termID, rows, cols })
        return
    }

    closePTY = async (termID: string): Promise<void> => {
        (await jsonRequest.post(this.formatMethodURL("/close"), { termID }))
    }

    allActivePTYs = async (): Promise<TerminalType[]> => {
        return (await jsonRequest.get(this.formatMethodURL("/all"))).data
    }
}
